package com.dyx.source.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author dengyouxu
 * @description TODO
 * @since 2021-05-04 下午3:28
 */
public class NioClient {

    public static void main(String[] args) throws Exception {
        //客户端通道
        SocketChannel socketChannel = SocketChannel.open(
                new InetSocketAddress("127.0.0.1", 8082));
        //设置为非阻塞模式
//        socketChannel.configureBlocking(false);
        //缓存区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //发送数据给服务端
        for (int i = 0; i < 10; i++) {

            buffer.put(("当前数值:" + i + "\n").getBytes());
            //切换读模式，读到通道中
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();
        }

        //关闭资源
        socketChannel.close();

    }
}
